package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.DeliveryDao;
import com.foreknow.elm.po.Delivery;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 啊滨
 * @date 2023/2/14 14:14
 */
public class DeliveryDaoImpl implements DeliveryDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Delivery> listDeliveryAddressByUserId(String userId) throws Exception {
        List<Delivery> list=new ArrayList<>();
        String sql="SELECT * FROM deliveryaddress WHERE userId=? GROUP BY daId";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setString(1,userId);
            rs=pst.executeQuery();
            while (rs.next()){
                Delivery delivery=new Delivery();
                delivery.setUserId(rs.getString("userId"));
                delivery.setDaId(rs.getInt("daId"));
                delivery.setContactName(rs.getString("contactName"));
                delivery.setContactTel(rs.getString("contactTel"));
                delivery.setContactSex(rs.getInt("contactSex"));
                delivery.setAddress(rs.getString("address"));
                list.add(delivery);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }

    @Override
    public Delivery getDeliveryAddressById(Integer daId) throws Exception {
        String sql="SELECT * FROM deliveryaddress WHERE daId=?";
        try {
            con=DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,daId);
            rs=pst.executeQuery();
            while (rs.next()){
                Delivery delivery=new Delivery();
                delivery.setUserId(rs.getString("userId"));
                delivery.setDaId(rs.getInt("daId"));
                delivery.setContactName(rs.getString("contactName"));
                delivery.setContactTel(rs.getString("contactTel"));
                delivery.setContactSex(rs.getInt("contactSex"));
                delivery.setAddress(rs.getString("address"));
                return delivery;
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return null;
    }

    @Override
    public int saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) throws Exception {
        int isRight=0;
        String sql="insert into deliveryaddress(contactName,contactSex,contactTel,address,userId) values(?,?,?,?,?)";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setString(1,contactName);
            pst.setInt(2,contactSex);
            pst.setString(3,contactTel);
            pst.setString(4,address);
            pst.setString(5,userId);
            isRight=pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return isRight;
    }

    @Override
    public int updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) throws Exception {
        int isRight=0;
        String sql="UPDATE deliveryaddress SET contactName=?,contactSex=?,contactTel=?,address=?,userId=? WHERE daId=?";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setString(1,contactName);
            pst.setInt(2,contactSex);
            pst.setString(3,contactTel);
            pst.setString(4,address);
            pst.setString(5,userId);
            pst.setInt(6,daId);
            isRight=pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return isRight;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) throws Exception {
        int isRight=0;
        String sql="DELETE FROM deliveryaddress WHERE daId=?";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setInt(1,daId);
            isRight=pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return isRight;
    }
}
